import os
import matplotlib.pyplot as plt
from BigDataWeb.algorithm import Algorithm


class DataVisualization(Algorithm):
    chart_png_dest_path = ""
    chart_title = ""
    x_label = ""
    x_values = []
    figure = None
    code = ""

    def __init__(self):
        Algorithm.__init__(self)
        self.will_create_field_desc_chart = False
        self.chart_png_dest_path = os.path.join(self.work_folder_path, "chart.png")
        
    def implent(self):
        # 执行算法
        Algorithm.implent(self)
        self.figure = plt.figure()
        
    def setXLabel(self, x_label):
        self.x_label = x_label
        self.x_values = self.data_source[self.x_label].values
    
    def saveToPng(self):
        self.figure.savefig(self.chart_png_dest_path)
        
    def prepareIpynbItems(self):
        Algorithm.prepareIpynbItems(self)
        self.ipynb_items["#code#"] = self.code
        self.ipynb_items["#chart_title#"] = self.chart_title
        self.ipynb_items["#x_label#"] = self.x_label
